<?php


namespace app\api\model;


use think\Collection;

class Product extends BaseModel
{
    protected $hidden
        = [
            'delete_time',
            'create_time',
            'update_time',
            'pivot',
            'from',
            'img_id'
        ];


    public function getMainImgUrlAttr($value, $data): string
    {
        return self::prefixImgUrl($value, $data);
    }
    public function imgsID(){
        return $this->hasMany(ProductImage::class,'product_id','id');
    }
    public function productProperty(){
        return $this->hasMany(ProductProperty::class,'product_id','id');
    }

    public static function getMostRecent($count): \think\Collection
    {
        return self::limit($count)->order('create_time desc')->select();
    }
    public static function getProductByCategoryID($categroy_id): Collection
    {
        return self::where('category_id','=',$categroy_id)->select();
    }
    public static function getProductDetail($id){
//        return self::with(['imgsID','imgsID.img','productProperty'])->find($id);
//        return self::with('productProperty')->with(
//            [
//                'imgsID' => function($query){
//                $query->with(['img'])->order('order','asc');
//                }
//            ])->find($id);
        //使用关联模式中排序
        return self::with([
            'imgsID'=>function($query){
                $query->with(['img'])->order('order','asc');
            },
            'productProperty'
        ])->find($id);
    }
}